package com.p2p.backend.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.p2p.backend.model.Privilege;

/**
 * leiwen@yiqihao.com
 */
public class MenuUtil {

	protected final static Logger logger = LoggerFactory
			.getLogger(MenuUtil.class);

	public static List<Map<String, Object>> buildRootList(
			List<Map<String, Object>> privilegeList) {
		List<Map<String, Object>> finalRootList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> rootList = findRootList(privilegeList);
		List<Map<String, Object>> notRootList = findNotRootList(privilegeList);
		for (Map<String, Object> root : rootList) {
			// 构造子结点
			buildChildList(root, notRootList);
			finalRootList.add(root);
		}
		return finalRootList;
	}
	
	public static List<Map<String, Object>> buildRootList2(
			List<Map<String, Object>> privilegeList) {
		List<Map<String, Object>> root = new ArrayList<Map<String, Object>>();
		Map<String,Map<String, Object>> allNodeMap = new HashMap<String,Map<String, Object>>();
		for(Map<String, Object> mapo:privilegeList){
			allNodeMap.put(mapo.get("acl").toString(), mapo);
		}
		build(allNodeMap,root);
		return root;
	}

	static int level = 1;

	// 为一个“root节点，这个地方的root指有孩子的节点”
	private static void buildChildList(Map<String, Object> rootNode,
			List<Map<String, Object>> notRootList) {
		Integer acl = getAcl(rootNode);
		for (Map<String, Object> notRoot : notRootList) {
			boolean equals = getParentAcl(notRoot).equals(acl);
			if (equals) {
				List<Map<String, Object>> list = (List<Map<String, Object>>) rootNode
						.get("children");
				if (list == null) {
					list = new ArrayList<Map<String, Object>>();
					rootNode.put("children", list);
				}
				list.add(notRoot);
				buildChildList(notRoot, notRootList);
			}
		}
	}

	private static Integer getAcl(Map<String, Object> notRoot) {
		return Integer.parseInt(notRoot.get("acl").toString());
	}

	private static Integer getParentAcl(Map<String, Object> p) {
		return Integer.parseInt(p.get("parent_acl").toString());
	}

	private static List<Map<String, Object>> findRootList(
			List<Map<String, Object>> list) {
		List<Map<String, Object>> rootList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> root : list) {
			if (getParentAcl(root) == -1) {
				rootList.add(root);
			}
		}
		
		return rootList;
	}

	private static List<Map<String, Object>> findNotRootList(
			List<Map<String, Object>> List) {
		List<Map<String, Object>> notRootList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> root : List) {
			if (getParentAcl(root) != -1) {
				notRootList.add(root);
			}
		}
		return notRootList;
	}

	// 数据处理，树中的字段，删除、重命名等
	public static void translate(List<Map<String, Object>> root) {
		for (Map<String, Object> map : root) {
			map.put("text", map.get("name"));
			map.put("href", map.get("url"));
			map.put("id", map.get("acl"));
			map.remove("name");
			map.remove("url");
			List<Map<String, Object>> children = (List<Map<String, Object>>) map
					.get("children");
			if (children != null) {
				translate(children);
			}
		}
	}

	
	public static void build(Map<String,Map<String, Object>> allNode,List<Map<String,Object>> root){
		for(Map<String, Object> node: allNode.values()){
			addInTree(node,root,allNode);
		}
	}
	public static void addInTree(Map<String, Object> node,List<Map<String,Object>> root,Map<String,Map<String, Object>> allNode){
		if(node.get("inTree")==null){
			String parent_acl = node.get("parent_acl").toString();
			if(parent_acl.equals("-1")){
				root.add(node);
				node.put("inTree", true);
			}else{
				Map<String,Object> father= allNode.get(parent_acl);
				if(father==null){
					return;
				}
				if(father.get("inTree")!=null){
					addChild(father,node);
					node.put("inTree", true);
				}else{
					addInTree(father,root,allNode);
					addChild(father,node);
					node.put("inTree", true);
				}
			}
		}
	}
	
	public static List<Map<String, Object>> buildList(
			List<Privilege> privilegeList) {
		List<Map<String, Object>> rootList = new ArrayList<Map<String, Object>>();
		Map<String, Map<String, Object>> rootMap = new HashMap<String, Map<String, Object>>();
		// 先计算level

		// 按照level排序
		for (Privilege privilege : privilegeList) {
			Integer acl = privilege.getAcl();
			Integer parentAcl = privilege.getParentAcl();
			Map<String, Object> newNode = createNode(acl, privilege.getName(),
					privilege.getUrl());
			rootMap.put(acl + "", newNode);
			if (parentAcl == null) {
				logger.error("parent_acl is null");
				continue;
			}
			if (parentAcl != -1) {
				rootList.add(newNode);
			} else {
				Map<String, Object> fatherNode = rootMap.get(parentAcl);
				addChild(fatherNode, newNode);
			}
		}
		return rootList;
	}

	private static Map<String, Object> createNode(Integer id, String text,
			String url) {
		Map<String, Object> node = new HashMap<String, Object>();
		node.put("id", id);
		node.put("text", text);
		if (url != null && !"".equals(url)) {
			node.put("href", url);
		}

		return node;
	}

	private static void addChild(Map<String, Object> father,
			Map<String, Object> child) {
		if (father == null) {
			logger.error("Father is null");
			return;
		}
		Object o = father.get("children");
		List<Map<String, Object>> childs;
		if (o == null) {
			childs = new ArrayList<Map<String, Object>>();
			father.put("children", childs);
		} else {
			childs = (List<Map<String, Object>>) o;
		}
		childs.add(child);
		return;
	}
}
